﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using BUS;

namespace QLDT
{
    public partial class frm_DMKhoaHoc : DevExpress.XtraEditors.XtraForm
    {
        public bool TTThemSua = true;
        private string makhoahoc_cu;
        public frm_DMKhoaHoc()
        {
            InitializeComponent();
        }
        public void load()
        {
            this.WindowState = FormWindowState.Maximized;
            layout_MaKhoa_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            layout_Makhoa_LookUp.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            gridDM_Khoahoc.DataSource = BUS_KhoaHoc.SelectKhoaHoc();
            txt_MaKH.Enabled = false;
            txt_NamBD.Enabled = false;
            txt_NamKT.Enabled = false;
            txt_SoLop.Enabled = false;
            txt_MaKhoa.Enabled = false;
            simpleBt_CapNhat.Enabled = simpleBt_Huy.Enabled = false;
            if (gv_Khoahoc.RowCount < 1)
            {
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = false;
            }
            else
                simpleBt_Sua.Enabled = simpleBt_Xoa.Enabled = true;
            simpleBt_Them.Enabled = true;
            Bindings();
            gridLookMaKhoa.Properties.View.OptionsBehavior.AutoPopulateColumns = false;
            gridLookMaKhoa.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            gridLookMaKhoa.Properties.ValueMember = "MaKhoa";
            gridLookMaKhoa.Properties.DisplayMember = "MaKhoa";
            gridLookMaKhoa.Properties.View.BestFitColumns();
            gridLookMaKhoa.Properties.PopupFormWidth = 400;
            gridLookMaKhoa.Properties.NullText = "Chọn khóa hoc";
            gridLookMaKhoa.Properties.DataSource = BUS_Khoa.SelectKhoa();
        }

        private void Bindings()
        {
            txt_MaKH.DataBindings.Clear();
            txt_MaKH.DataBindings.Add("Text", gv_Khoahoc.DataSource, "MaKH");

            txt_NamBD.DataBindings.Clear();
            txt_NamBD.DataBindings.Add("Text", gv_Khoahoc.DataSource, "NamBD");

            txt_NamKT.DataBindings.Clear();
            txt_NamKT.DataBindings.Add("Text", gv_Khoahoc.DataSource, "NamKT");

            txt_SoLop.DataBindings.Clear();
            txt_SoLop.DataBindings.Add("Text", gv_Khoahoc.DataSource, "SoLop");

            txt_MaKhoa.DataBindings.Clear();
            txt_MaKhoa.DataBindings.Add("Text", gv_Khoahoc.DataSource, "MaKhoa");
        }
        private void simpleBt_Them_Click(object sender, EventArgs e)
        {
            layout_MaKhoa_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layout_Makhoa_LookUp.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            simpleBt_Them.Enabled = false;
            simpleBt_Sua.Enabled = false;
            simpleBt_Xoa.Enabled = false;
            simpleBt_CapNhat.Enabled = true;
            simpleBt_Huy.Enabled = true;

            txt_MaKH.DataBindings.Clear();
            txt_NamBD.DataBindings.Clear();
            txt_NamKT.DataBindings.Clear();
            txt_SoLop.DataBindings.Clear();
            txt_MaKhoa.DataBindings.Clear();

            txt_MaKH.Text = "";
            txt_NamBD.Text = "";
            txt_NamKT.Text = "";
            txt_SoLop.Text = "";
            txt_MaKhoa.Text = "";

            txt_MaKH.Enabled = true;
            txt_NamBD.Enabled = true;
            txt_NamKT.Enabled = true;
            txt_SoLop.Enabled = true;
            txt_MaKhoa.Enabled = true;

            TTThemSua = true;
        }

        private void simpleBt_Xoa_Click(object sender, EventArgs e)
        {
            bool kq;
            if (MessageBox.Show("Bạn có chắc sẽ xóa lớp " + txt_MaKH.Text + " hay không, nếu xóa thì toàn bộ thông thông tin liên quan sẽ mất.", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                kq = BUS_KhoaHoc.deleteKhoaHoc(txt_MaKH.Text);
                if (!kq)
                    XtraMessageBox.Show("Xóa thất bại", "Thông báo");
                else
                {
                    load();
                }
            }
            else
                load();
        }

        private void simpleBt_Sua_Click(object sender, EventArgs e)
        {
            layout_MaKhoa_txt.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
            layout_Makhoa_LookUp.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
            makhoahoc_cu = txt_MaKH.Text;
            simpleBt_Them.Enabled = false;
            simpleBt_Sua.Enabled = false;
            simpleBt_Xoa.Enabled = false;
            simpleBt_CapNhat.Enabled = true;
            simpleBt_Huy.Enabled = true;

            txt_MaKH.DataBindings.Clear();
            txt_NamBD.DataBindings.Clear();
            txt_NamKT.DataBindings.Clear();   
            txt_SoLop.DataBindings.Clear();
            txt_MaKhoa.DataBindings.Clear();
           
            txt_MaKH.Enabled = true;
            txt_NamBD.Enabled = true;
            txt_NamKT.Enabled = true;
            txt_SoLop.Enabled = true;
            txt_MaKhoa.Enabled = true;
            TTThemSua = false;
        }
        public bool KTDulieu()
        {
            if (txt_MaKH.Text == "")
                return false;
            if (txt_NamBD.Text == "")
                return false;
            if (txt_NamKT.Text == "")
                return false;
            if (txt_SoLop.Text == "")
                return false;
            if (gridLookMaKhoa.EditValue == null)
                return false;
            return true;

        }


        private void frm_DMKhoaHoc_Load(object sender, EventArgs e)
        {
            load();
        }

        private void simpleBt_Huy_Click(object sender, EventArgs e)
        {
            load();
        }

        private void simpleBt_CapNhat_Click(object sender, EventArgs e)
        {
            if (!KTDulieu())
            {
                XtraMessageBox.Show("Nhập đầy đủ nga", "Thông báo");
                return;
            }
            DTO_KhoaHoc khoahoc = new DTO_KhoaHoc();
            khoahoc.MaKH = txt_MaKH.Text;
            khoahoc.NamBD = int.Parse(txt_NamBD.Text);
            khoahoc.NamKT = int.Parse(txt_NamKT.Text);
            khoahoc.SoLop = int.Parse(txt_SoLop.Text);
            khoahoc.MaKhoa = gridLookMaKhoa.EditValue.ToString();
            if (TTThemSua)
            {

                if (BUS_KhoaHoc.insertKhoaHoc(khoahoc))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }
            else
            {
                if (BUS_KhoaHoc.updateKhoaHoc(khoahoc, makhoahoc_cu))
                    load();
                else
                    XtraMessageBox.Show("lỗi từ hệ thống, hãy liên hệ với bộ phận kỹ thuật, mã lỗi [simpleBt_CapNhat_Click]", "Thông báo");
            }
        }

        private void txt_MaKH_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_NamBD.Focus();
        }

        private void txt_NamBD_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_NamKT.Focus();
        }

        private void txt_NamKT_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_SoLop.Focus();
        }

        private void txt_SoLop_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txt_MaKH.Focus();
        }

        private void txt_SoLop_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
           && !char.IsDigit(e.KeyChar)
           && e.KeyChar != '.')
            {
                e.Handled = true;
            }


            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }

        }

        private void txt_NamBD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
           && !char.IsDigit(e.KeyChar)
           && e.KeyChar != '.')
            {
                e.Handled = true;
            }


            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }

        }

        private void txt_NamKT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
           && !char.IsDigit(e.KeyChar)
           && e.KeyChar != '.')
            {
                e.Handled = true;
            }


            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }

        }



        
    }
}